package com.zmq.utils.redis.impl;

import com.zmq.utils.redis.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service("redireService")
public class RedisServiceImpl implements IRedisService {

    //注入RedisTemplate模板
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key,value);
    }

    @Override
    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    @Override
    public void del(String key) {
        redisTemplate.delete(key)
;    }

    @Override
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public void setExpire(String key, long time, TimeUnit unit) {
        redisTemplate.expire(key,time,unit);
    }

    @Override
    public Set obscureKey(String key) {
        return redisTemplate.keys(key);
    }

    /**
     * 以增量的方式将long值存储在变量中。
     * @param key
     */
    @Override
    public void onLineCounter(String key) {
        redisTemplate.opsForValue().increment(key,1L);
    }
}
